//
//  ZCDiscoverVC.m
//  SSMusic
//
//  Created by 张灿 on 16/4/18.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCDiscoverVC.h"
#import "ZCTextField.h"
#import "RecommendationTVC.h"
#import "SongListCVC.h"
#import "AnchorStationVC.h"
#import "RankListVC.h"

#define ZCScreenW [UIScreen mainScreen].bounds.size.width
#define ZCScreenH [UIScreen mainScreen].bounds.size.height


@interface ZCDiscoverVC ()<UIScrollViewDelegate>

//标题scroll
@property (weak, nonatomic) UIScrollView *titleScroll;

//内容scroll
@property (weak, nonatomic) UIScrollView *contentScroll;

//定义按钮保存按钮属性变化
@property (weak, nonatomic) UIButton *selectBtn;

//创建可变数组保存所有按钮
@property (nonatomic, strong) NSMutableArray *titleBtns;

//底部红线
@property (weak, nonatomic) UIView *underLine;

// 记录上一次内容滚动视图偏移量
@property (nonatomic, assign) CGFloat lastOffsetX;

@end

@implementation ZCDiscoverVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //设置标题scroll
    [self setupTitleScroll];
    
    //设置内容scroll
    [self setupContentScroll];
    
    //添加所有子控制器
    [self setupAllChildVC];
    
    //添加所有标题按钮
    [self setupAllTitle];
    
    //添加红线
//    [self addRedLine];
    
    // 导航控制器下，scrollView顶部会自动添加额外滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;

}

//- (void)addRedLine
//{
//    CGFloat w = ZCScreenW / self.titleBtns.count;
//    CGFloat h = 2;
//    CGFloat y = CGRectGetMaxY(self.titleScroll.frame) - h;
//    CGFloat x = 0;
//    self.underLine.frame = CGRectMake(x, y, w, h);
//}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    

    //左侧听歌识曲
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"cm2_topbar_icn_mic"] highlightImage:[UIImage imageNamed:@"cm2_topbar_icn_mic_prs"] Target:self action:@selector(leftClick)];
    
    //中间搜索框
    ZCTextField *searchV = [[ZCTextField alloc] initWithFrame:CGRectMake(0, 0, ZCScreenW - 100, 30)];
    self.navigationItem.titleView = searchV;

}


//听歌识别
- (void)leftClick
{
    NSLog(@"听歌识别");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置主页导航框架

- (NSMutableArray *)titleBtns
{
    if (_titleBtns == nil) {
        _titleBtns = [NSMutableArray array];
    }
    return _titleBtns;
}

- (void)setupTitleScroll
{
    UIScrollView *titleScroll = [[UIScrollView alloc] init];
    titleScroll.frame = CGRectMake(0, 0, ZCScreenW, 35);
    [self.view addSubview:titleScroll];
    _titleScroll = titleScroll;
}

- (void)setupContentScroll
{
    UIScrollView *contentScroll = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(_titleScroll.frame);
    contentScroll.frame = CGRectMake(0, y, ZCScreenW, ZCScreenH - y);
//    contentScroll.alwaysBounceHorizontal = NO;
    //取消内容的弹簧效果
    contentScroll.bounces = NO;
    [self.view addSubview:contentScroll];
    contentScroll.delegate = self;
    _contentScroll = contentScroll;
    
}

- (void)setupAllChildVC
{
    //个性推荐
    RecommendationTVC *rtvc = [[RecommendationTVC alloc] init];
    rtvc.title = @"个性推荐";
    [self addChildViewController:rtvc];
    
    //歌单
    SongListCVC *scvc = [[SongListCVC alloc] init];
    scvc.title = @"歌单";
    [self addChildViewController:scvc];
    
    //主播电台
    AnchorStationVC *asvc = [[AnchorStationVC alloc] init];
    asvc.title = @"主播电台";
    [self addChildViewController:asvc];
    
    //排行榜
    RankListVC *rvc = [[RankListVC alloc] init];
    rvc.title = @"排行榜";
    [self addChildViewController:rvc];
    
}

- (void)setupAllTitle
{
    NSInteger count = self.childViewControllers.count;
    CGFloat x = 0;
    CGFloat btnW = ZCScreenW / count;
    for (int i = 0; i < count; i++) {
        //取出对应的子控制器
        UIViewController *vc = self.childViewControllers[i];
        //创建自定义按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置常态下的文字
        [btn setTitle:vc.title forState:UIControlStateNormal];
        
        //设置常态下的颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        //设置字体大小
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        //tag值
        btn.tag = i;
        
        //frame
        x = i * btnW;
        
        
        btn.frame = CGRectMake(x, 0, btnW, 35);
        
        //添加到titlescroll
        [_titleScroll addSubview:btn];
        
        //监听按钮点击
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //把创建的按钮都添加到数组
        [self.titleBtns addObject:btn];
        
        //默认选中第一个按钮
        if (i == 0) {
            [self btnClick:btn];
            //创建下标
            UIView *underline = [[UIView alloc] init];
            underline.backgroundColor = [UIColor redColor];
            underline.width = [vc.title sizeWithFont:[UIFont systemFontOfSize:15]].width;
            underline.height = 2;
            underline.y = self.titleScroll.height - underline.height;
            underline.centerX = btn.centerX;
            [self.titleScroll addSubview:underline];
            _underLine = underline;
        }
    }
    
    //设置标题滚动范围
    self.titleScroll.contentSize = CGSizeMake(count * btnW, 0);
    _titleScroll.showsHorizontalScrollIndicator = NO;
    
    //设置内容滚动范围
    _contentScroll.contentSize = CGSizeMake(count * ZCScreenW, 0);
    _contentScroll.pagingEnabled = YES;
    _contentScroll.showsHorizontalScrollIndicator = NO;
}

// 点击标题的时候就会调用
- (void)btnClick:(UIButton *)button
{
    NSInteger i = button.tag;
    // 1.标题颜色变成红色
    [self selButton:button];
    
    
    // 2.把对应的子控制器的view添加上去
    [self setupChildControllerView:i];
        
    // 3.滚动到对应的位置
    CGFloat x = i * ZCScreenW;
    _contentScroll.contentOffset = CGPointMake(x, 0);
    
}

//添加子控制器到contentScroll
- (void)setupChildControllerView:(NSInteger)i
{
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) return;
    
    CGFloat x = i * ZCScreenW;
    vc.view.frame = CGRectMake(x, 0, _contentScroll.bounds.size.width, _contentScroll.bounds.size.height);
    [_contentScroll addSubview:vc.view];}

//按钮选中做的操作
- (void)selButton:(UIButton *)btn
{
    //移动下标 注意!!!一定要先设置尺寸在设置中心X
    [UIView animateWithDuration:0.25 animations:^{
        self.underLine.width = [btn.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:15]].width;
        self.underLine.centerX = btn.centerX;
    }];
    //让之前的按钮变黑
    self.selectBtn.selected = NO;
    
    //让传进来的按钮变红
    btn.selected = YES;
    
    //记录传进来的按钮
    self.selectBtn = btn;
    
}

////设置下标偏移量
//- (void)setupUnderLineOffset:(UIButton *)btn
//{
//    //移动下标
//    [UIView animateWithDuration:0.25 animations:^{
//        self.underLine.centerX = btn.centerX;
//    }];
//}

#pragma mark - Scroll代理

//停止滚动的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取当前是第几页
    NSInteger page = scrollView.contentOffset.x / ZCScreenW;
    
    //根据页数取出对应的按钮
    UIButton *btn = self.titleBtns[page];
    
    //设置按钮颜色
    [self selButton:btn];
    
    //设置应该加载的View
    [self setupChildControllerView:page];

}

//滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}



//// 设置下标偏移
//- (void)setUpUnderLineOffset:(CGFloat)offsetX rightLabel:(UILabel *)rightLabel leftLabel:(UILabel *)leftLabel
//{
////    if (_isClickTitle) return;
//    
//    // 获取两个标题中心点距离
//    CGFloat centerDelta = rightLabel.x - leftLabel.x;
//    
//    // 标题宽度差值
//    CGFloat widthDelta = [self widthDeltaWithRightLabel:rightLabel leftLabel:leftLabel];
//    
//    // 获取移动距离
//    CGFloat offsetDelta = offsetX - _lastOffsetX;
//    
//    // 计算当前下划线偏移量
//    CGFloat underLineTransformX = offsetDelta * centerDelta / ZCScreenW;
//    
//    // 宽度递增偏移量
//    CGFloat underLineWidth = offsetDelta * widthDelta / ZCScreenW;
//    
//    self.underLine.width += underLineWidth;
//    self.underLine.x += underLineTransformX;
//    
//    
//    
//}
//
//// 获取两个标题按钮宽度差值
//- (CGFloat)widthDeltaWithRightLabel:(UILabel *)rightLabel leftLabel:(UILabel *)leftLabel
//{
//    CGRect titleBoundsR = [rightLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:YZTitleFont} context:nil];
//    
//    CGRect titleBoundsL = [leftLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:YZTitleFont} context:nil];
//    
//    return titleBoundsR.size.width - titleBoundsL.size.width;
//}






@end
